/**
 * @author scylla
 * @version V1.0
 * @Project echo-java-base
 * @Title 圆形Set
 * @Description *
 * @Package concurrent.mthread.bf
 * @Since 2021/8/13 4:55 下午
 */
package concurrent.mthread.bf;

public class CircularSet {

    private int[] array;
    private int len;
    private int index = 0;

    //构造
    public CircularSet(int size) {
        this.len = size;
        array = new int[size];
        //初始化所有的set
        for (int i = 0; i < size; i++) {
            array[i] = -1;
        }
    }

    public synchronized void add(int i) {
        array[index] = i;
        //wrap index and write over old elements;
        index = ++index % len;
    }

    public synchronized boolean contains(int val) {
        for (int i = 0; i < len; i++) {
            if (array[i] == val) return true;
        }
        //全部不包含则返回false
        return false;
    }



}
